#ifndef UART_H_
#define UART_H_

#include <stdbool.h>
#include <stdint.h>
#include "msp.h"

#define RX_BUF_LEN 128
#define RX_MASK 0x7F
#define FRAME_SIZE 20           // max is defined in smpl_nwk_config
#define FRAME_DATA_SIZE 16      // number of data bytes in a frame
#define SS 0x55                 // Start sentinel
#define ES 0xAA                 // End sentinel


/**initialize_UART initializes the uart subsystem by setting the appropriate registers
Currently, it sets the baud rate to 115200, when the master clock is at 12 Mhz.*/
void initialize_UART();
/**
send_uart_data sends a buffer of bytes over the uart port
@precondition - initialize_UART must be called before using this function
@param buffer - a pointer to the buffer that the user wants to send
@param length - the length of the buffer that the user wants to send. 
If length > FRAME_DATA_SIZE then send_uart_data only sends FRAME_DATA_SIZE bytes
It will not send more than FRAM_DATA_SIZE bytes in a single call*/
void send_uart_data(uint8_t *buffer, uint8_t length);
#define uart_send send_uart_data
/**
uart_recieve and it's blocking variant, uart_recieve_blocking 
receive FRAME_DATA_SIZE packets from the controling computer via the uart port. 
Since all data is sent and receieved in packets, if a packet has an error
(only missing byte errors are detected) then the packet is discarded

@return uint16_t counting the number of data packets received since the start of
execution. You can use this to tell whether or not a new packet was received
@param buffer - a pointer to a buffer where the receive routine can
place received data
NB: There is no explicit timeout check.
 */
uint16_t uart_receive (uint8_t *buffer);
//This function will block until len bytes come down the wire.
uint16_t uart_receive_blocking (uint8_t *buffer);

#endif /*UART_H_*/
